home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Games / Pentominoes 2.0 / Shell ƒ / text layer.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-29  |  15.2 KB  |  200 lines  |  [TEXT/MMCC]

  1. #include "text layer.h"
  2. #include "program globals.h"
  3. #if USE_DRAG
  4. #include "drag layer.h"
  5. #endif
  6. #include "window layer.h"
  7. #include "memory layer.h"
  8. #include <FixMath.h>
  9.  
  10. #define RequireWindow(x)    if (theWindow==0L) return x
  11. #define RequireTE(x)        {    \
  12.                                 hTE=GetWindowTE(theWindow);    \
  13.                                 if (hTE==0L)    return x;    }
  14.  
  15. enum { key_LeftArrow=0x1c, key_RightArrow, key_UpArrow, key_DownArrow };
  16.  
  17. Boolean AnyTextInScrapQQ(void)
  18. {
  19.     long            dummy;
  20.     
  21.     LoadScrap();
  22.     return (GetScrap(0L, 'TEXT', &dummy)!=noTypeErr);
  23. }
  24.  
  25. void SetTheText(WindowRef theWindow, Ptr data, long count)
  26. {
  27.     TEHandle        hTE;
  28.     ControlHandle    vScrollBar;
  29.     
  30.     RequireWindow();
  31.     RequireTE();
  32.     vScrollBar=GetWindowVScrollBar(theWindow);
  33.     
  34.     TESetText(data, count, hTE);
  35.     TESetSelect(0, 0, hTE);
  36.     if (vScrollBar!=0L)
  37.         AdjustVScrollBar(vScrollBar, hTE);
  38. #if USE_DRAG
  39.     ResetHiliteRgn(theWindow);
  40. #endif
  41. }
  42.  
  43. Boolean AnyTextQQ(WindowRef theWindow)
  44. {
  45.     TEHandle        hTE;
  46.     
  47.     RequireWindow(FALSE);
  48.     RequireTE(FALSE);
  49.     
  50.     return ((**hTE).teLength!=0);
  51. }
  52.  
  53. Boolean AnyHighlightedQQ(WindowRef theWindow)
  54. {
  55.     TEHandle        hTE;
  56.     
  57.     RequireWindow(FALSE);
  58.     RequireTE(FALSE);
  59.     
  60.     return ((**hTE).selStart!=(**hTE).selEnd);
  61. }
  62.  
  63. short SelectionStart(WindowRef theWindow)
  64. {
  65.     TEHandle        hTE;
  66.     
  67.     RequireWindow(-1);
  68.     RequireTE(-1);
  69.     
  70.     return (**hTE).selStart;
  71. }
  72.  
  73. short SelectionEnd(WindowRef theWindow)
  74. {
  75.     TEHandle        hTE;
  76.     
  77.     RequireWindow(-1);
  78.     RequireTE(-1);
  79.     
  80.     return (**hTE).selEnd;
  81. }
  82.  
  83. Boolean InsertBeforeStart(WindowRef theWindow, Str255 theStr)
  84. {
  85.     TEHandle        hTE;
  86.     unsigned long    len;
  87.     unsigned long    oldLen;
  88.     
  89.     RequireWindow(FALSE);
  90.     RequireTE(FALSE);
  91.     
  92.     len=theStr[0];
  93.     hTE=GetWindowTE(theWindow);
  94.     oldLen=(**hTE).teLength;
  95.     if (oldLen+len>32767)
  96.         return FALSE;
  97.     
  98.     TEInsert(&theStr[1], len, hTE);
  99.     SetWindowIsModified(theWindow, TRUE);
  100. #if USE_DRAG
  101.     ResetHiliteRgn(theWindow);
  102. #endif
  103.     return TRUE;
  104. }
  105.  
  106. Boolean InsertAfterEnd(WindowRef theWindow, Str255 theStr)
  107. {
  108.     TEHandle        hTE;
  109.     unsigned long    len;
  110.     unsigned long    oldLen;
  111.     unsigned long    oldSelStart;
  112.     unsigned long    oldSelEnd;
  113.     
  114.     RequireWindow(FALSE);
  115.     RequireTE(FALSE);
  116.     
  117.     len=theStr[0];
  118.     hTE=GetWindowTE(theWindow);
  119.     oldLen=(**hTE).teLength;
  120.     if (oldLen+len>32767)
  121.         return FALSE;
  122.     
  123.     oldSelStart=(**hTE).selStart;
  124.     oldSelEnd=(**hTE).selEnd;
  125.     TESetSelect(oldSelEnd, oldSelEnd, hTE);
  126.     TEInsert(&theStr[1], len, hTE);
  127.     TESetSelect(oldSelStart, oldSelEnd, hTE);
  128.     SetWindowIsModified(theWindow, TRUE);
  129. #if USE_DRAG
  130.     ResetHiliteRgn(theWindow);
  131. #endif
  132.     return TRUE;
  133. }
  134.  
  135. void GetSelectionString(WindowRef theWindow, Str255 theStr)
  136. {
  137.     TEHandle        hTE;
  138.     short            selStart, selEnd;
  139.     
  140.     RequireWindow();
  141.     RequireTE();
  142.     
  143.     selStart=SelectionStart(theWindow);
  144.     selEnd=SelectionEnd(theWindow);
  145.     if (selEnd-selStart>255)
  146.         selEnd=selStart+255;
  147.     Mymemcpy((Ptr)&theStr[1], (Ptr)((unsigned long)(*((**hTE).hText))+selStart), selEnd-selStart);
  148.     theStr[0]=selEnd-selStart;
  149. }
  150.  
  151. short TotalNumberOfLines(TEHandle hTE)
  152. {
  153.     short            numLines;
  154.     
  155.     if (hTE==0L)
  156.         return -1;
  157.     
  158.     numLines=(**hTE).nLines;
  159.     if (*((unsigned char*)((long)(*((**hTE).hText))+(**hTE).teLength-1))==0x0d)
  160.         numLines++;
  161.     
  162.     return numLines;
  163. }
  164. void HandleShiftArrow(TEHandle hTE, unsigned char theChar)
  165. {
  166.     short            offset, destOffset;
  167.     short            lineNum, lineStart;
  168.     short            offsetFromLineStart;
  169.     
  170.     switch (theChar)
  171.     {
  172.         case key_UpArrow:
  173.             offset=(**hTE).selStart;
  174.             lineNum=LineNumberFromOffset(hTE, offset);
  175.             if (lineNum==0)
  176.                 destOffset=0;
  177.             else
  178.             {
  179.                 lineStart=LineStart(hTE, lineNum);
  180.                 offsetFromLineStart=offset-lineStart;
  181.                 if ((offset==(**hTE).teLength) && ((*((**hTE).hText))[offset-1]=='\r'))
  182.                     destOffset=LineStart(hTE, lineNum);
  183.                 else
  184.                 {
  185.                     destOffset=LineStart(hTE, lineNum-1)+offsetFromLineStart;
  186.                     if (destOffset>=lineStart)
  187.                         destOffset=lineStart-1;
  188.                 }
  189.             }
  190.             if (destOffset!=offset)
  191.                 TESetSelect(destOffset, (**hTE).selEnd, hTE);
  192.             break;
  193.         case key_DownArrow:
  194.             offset=(**hTE).selEnd;
  195.             lineNum=LineNumberFromOffset(hTE, offset);
  196.             lineStart=LineStart(hTE, lineNum);
  197.             offsetFromLineStart=offset-lineStart;
  198.             destOffset=LineStart(hTE, lineNum+1)+offsetFromLineStart;
  199.             if (destOffset>=(**hTE).teLength)
  200.                 destOffset=(*